// utils/request.js
const baseURL = 'http://localhost:8081/api'

// utils/request.js
export const request = (options) => {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('token')
    const userId = uni.getStorageSync('userId')

    console.log('[request]', {
      token,
      userId,
      options
    })
	  uni.request({
	    ...options,
	    header: {
	      'Content-Type': 'application/json',
	      'Authorization': token,
	      'User-Id': userId, // 改为 User-Id
	      ...options.header
	    },
      success: (res) => {
        console.log('[response]', res)
        
        if (res.statusCode === 401) {
          uni.redirectTo({ url: '/pages/login/login' })
          reject(new Error('请重新登录'))
          return
        }

        if (res.statusCode === 200) {
          resolve(res.data)
        } else {
          reject(new Error(res.data?.message || '请求失败'))
        }
      },
      fail: (err) => {
        console.error('[request fail]', err)
        reject(err)
      }
    })
  })
}

// 导出默认对象
export default {
  request
}